import React from 'react'
import { Switch, Route, Redirect } from "react-router-dom"
import { RouterItem } from "./routerconfig"
//定义一个接口
interface Iprops {
    routes: RouterItem[]
}
//函数式组件
export default function routerView({ routes }: Iprops) {
    let routesArr = routes.filter((item: RouterItem) => item.component)
    let redirectArr = routes.filter(item => item.redirect)
    return (
        <Switch>
            {routesArr.length && routesArr.map((item: RouterItem) => <Route key={item.path} path={item.path}
                render={(props) => {
                    if (item.children) {
                        return <item.component routes={item.children} {...props} />
                    }
                    return <item.component {...props}/>
                }}></Route>)}
            {
                redirectArr.map((item:RouterItem)=>{
                    return <Redirect key={item.path} from={item.path} to={item.redirect}/>
                })
            }
        </Switch>
    )
}
